package com.findme.fecedetection.ui.activity

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.findme.fecedetection.R
import com.findme.fecedetection.adapter.UploadingAdapter
import com.findme.fecedetection.base.ImmersionTitleActivity
import com.findme.fecedetection.net.HttpManager
import com.findme.fecedetection.ui.window.OnePickerViewPopupWindow
import com.findme.fecedetection.utlis.Data
import com.tanguy.user.utlis.extend.click
import com.tanguy.user.utlis.extend.requestSucceed
import com.tanguy.user.utlis.extend.setText
import com.tanguy.user.utlis.extend.showPopupWindow
import com.yanzhenjie.album.Album
import org.jetbrains.anko.find

/**
 * Created by dab on 2017/11/23 0023 20:29
 */
class AddInformationActivity : ImmersionTitleActivity() {
    override fun setContentViewRes(): Int = R.layout.activity_add_information
    val data = ArrayList<Any>()
    var mPathList = ArrayList<String>()
    private val uploadingAdapter = UploadingAdapter(data)
    override fun initView() {
        super.initView()
        data.add(R.mipmap.btn_feedback_imageadd1)
        click(R.id.iv_back) { finish() }
        find<RecyclerView>(R.id.rv_picture).apply {
            layoutManager = LinearLayoutManager(this@AddInformationActivity)
            adapter = uploadingAdapter
        }
    }

    override fun initData() {
        super.initData()
        HttpManager.address().requestSucceed(this) {
            val city = it.get("province")?.asString + it.get("city")?.asString
            setText(R.id.tv_location, city)
        }
    }

    override fun initEvent() {
        super.initEvent()
        uploadingAdapter.onDelete = { delete, position ->
            if (delete) {
                mPathList.remove(data[position])
                data.removeAt(position)
                if (data.size == 9) {
                    data.add(R.mipmap.btn_feedback_imageadd1)
                }
                uploadingAdapter.notifyDataSetChanged()
            } else {
                Album.album(this)
                        .title("图库") // 配置title。
                        .selectCount(10) // 最多选择几张图片。
                        .columnCount(4) // 相册展示列数，默认是2列。
                        .camera(true) // 是否有拍照功能。
                        .checkedList(mPathList) // 已经选择过得图片，相册会自动选中选过的图片，并计数。
                        .start(999) // 999是请求码，返回时onActivityResult()的第一个参数。
            }
        }
        //性别
        click(R.id.tv_kind) {
            showPopupWindow(OnePickerViewPopupWindow(this, Data.getKind()) { _, msg ->
                setText(R.id.tv_kind, msg!!)
            })
        }
        click(R.id.tv_size) {
            showPopupWindow(OnePickerViewPopupWindow(this, Data.getSize()) { _, msg ->
                setText(R.id.tv_size, msg!!)
            })
        }
        click(R.id.tv_weight) {
            showPopupWindow(OnePickerViewPopupWindow(this, Data.getWeight()) { _, msg ->
                setText(R.id.tv_weight, msg!!)
            })
        }
        click(R.id.tv_colour) {
            showPopupWindow(OnePickerViewPopupWindow(this, Data.getColour()) { _, msg ->
                setText(R.id.tv_colour, msg!!)
            })
        }
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data1: Intent?) {
        super.onActivityResult(requestCode, resultCode, data1)
        if (requestCode == 999) {
            if (resultCode == RESULT_OK) { // Successfully.
                mPathList = Album.parseResult(data1)
                data.clear()
                data.addAll(mPathList)
                if (data.size < 10) {
                    data.add(R.mipmap.btn_feedback_imageadd1)
                }
                uploadingAdapter.notifyDataSetChanged()
            } else if (resultCode == RESULT_CANCELED) { // User canceled.
                // 用户取消了操作。
            }
        }
    }
}